package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xin.di.uav.applet.mapper.RoleMapper;
import com.xin.di.uav.applet.mapper.SysRolePermissionMapper;
import com.xin.di.uav.applet.request.RoleAddParam;
import com.xin.di.uav.applet.request.RoleEditParam;
import com.xin.di.uav.applet.resp.SysRoleResp;
import com.xin.di.uav.applet.service.RoleService;
import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.common.utils.IdUtils;
import com.xin.di.uav.operate.mapper.UserRoleMapper;
import com.xin.di.uav.operate.po.SysRolePO;
import com.xin.di.uav.operate.po.SysRolePermissionPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public SysRoleResp list(int pageNum, int pageSize, String roleName) {
        SysRoleResp sysRoleResp = new SysRoleResp();
        pageNum = (pageNum - 1) * pageSize;
        int count = roleMapper.count(roleName);
        if (count > 0) {
            List<SysRolePO> sysRolePOS = roleMapper.selectList(roleName, pageNum, pageSize);
            List<String> roleIds = sysRolePOS.stream().map(SysRolePO::getId).collect(Collectors.toList());
            List<SysRolePermissionPO> sysRolePermissionPOS = sysRolePermissionMapper.selByIds(roleIds);
            List<SysRoleResp.Record> recordList = new ArrayList<>();
            for (SysRolePO sysRolePO : sysRolePOS) {
                SysRoleResp.Record record = BeanUtil.copyProperties(sysRolePO, SysRoleResp.Record.class);
                List<SysRolePermissionPO> sysRolePermissionPOS1 = sysRolePermissionPOS.stream().filter(sysRolePermissionPO -> sysRolePermissionPO.getRoleId().equals(sysRolePO.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(sysRolePermissionPOS1)) {
                    List<String> permissionIds = sysRolePermissionPOS1.stream().map(SysRolePermissionPO::getPermissionId).collect(Collectors.toList());
                    record.setRolePermissions(permissionIds);
                }
                recordList.add(record);
            }

            sysRoleResp.setRecordList(recordList);
        }

        return sysRoleResp;
    }

    @Override
    public List<IdAndNameResp> selList(String name) {
        return roleMapper.selIdAndName(name);
    }

    @Override
    @Transactional
    public Boolean add(RoleAddParam roleAddParam) {
        SysRolePO sysRolePO = BeanUtil.copyProperties(roleAddParam, SysRolePO.class);
        sysRolePO.setId(IdUtils.getId());
        roleMapper.insert(sysRolePO);
        List<String> checkedIds = roleAddParam.getCheckedIds();
        if (CollectionUtil.isNotEmpty(checkedIds)) {
            for (String checkedId : checkedIds) {
                SysRolePermissionPO sysRolePermissionPO = new SysRolePermissionPO();
                sysRolePermissionPO.setPermissionId(checkedId);
                sysRolePermissionPO.setRoleId(sysRolePO.getId());
                sysRolePermissionMapper.insert(sysRolePermissionPO);
            }
        }
        return null;
    }

    @Override
    public Boolean edit(RoleEditParam roleEditParam) {
        return null;
    }

    @Override
    @Transactional
    public boolean delete(String id) {
        roleMapper.deleteById(id);
        sysRolePermissionMapper.deleteByRoleId(id);
        userRoleMapper.deleteByRoleId(id);
        return true;
    }
}
